import Nunjucks from "nunjucks";
import { fileURLToPath } from "url";

export const regTemplate = () => {
  return async (ctx, next) => {
    ctx.render = function (html, obj) {
      //当前文件的可被浏览器所解析的绝对路径
      let currenFileUrl = import.meta.url;
      //   console.log(currenFileUrl);

      let viewBaseUrl = new URL("../views", currenFileUrl);
      // console.log(viewBaseUrl);

      let viewBasePath = fileURLToPath(viewBaseUrl);
      // console.log(viewBasePath);

      let env = Nunjucks.configure(viewBasePath);
      // console.log(env);

      ctx.response.body = env.render(html, obj);
      // env.render(html, obj);
    };
    await next();
  };
};
